from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import redirect
from common.models import User, Complain, Result
from static.func.my_func import test

# Create your views here.

def list(request):
    username = request.session.get('username', '游客')
    user = User.objects.filter(username=username)
    if user:
        complainList = user[0].complain_set.all()
        return render(request, 'customer/list.html', {'complains': complainList, 'username': username})
    else:
        return redirect('/common/login')

def info(request):
    username = request.session.get('username', '游客')
    try:
        user = User.objects.get(username=username)
        first_name = user.first_name
        last_name = user.last_name
        id_number = user.id_number
        address = user.address
        email = user.email
        mobile = user.mobile
        return render(request, 'customer/info.html', {'username': username, 'first_name': first_name, 'last_name': last_name, 'id_number': id_number, 'address': address, 'email': email, 'mobile': mobile})
    except:
        return redirect('/common/login')


def showinfo(request):
    # 获取表单信息
    username = request.POST.get('username')
    first_name = request.POST.get('first_name')
    last_name = request.POST.get('last_name')
    id_number = request.POST.get('id_number')
    address = request.POST.get('address')
    email = request.POST.get('email')
    mobile = request.POST.get('mobile')
    # 更新
    user = User.objects.get(username=username)
    user.username = username
    user.first_name = first_name
    user.last_name = last_name
    user.id_number = id_number
    user.address = address
    user.email = email
    user.mobile = mobile
    user.save()

    return redirect('/customer/info')

def detail(request):
    username = request.session.get('username', '游客')
    title = request.GET.get('title')
    complain = Complain.objects.get(title=title)
    try:
        result = complain.result
        return render(request, 'customer/detail.html', {'complain': complain, 'result': result.content, 'username': username})
    except:
        return render(request, 'customer/detail.html', {'complain': complain,  'result': '暂无处理结果', 'username': username})

def complain(request):
    username = request.session.get('username', '游客')
    try:
        user = User.objects.get(username=username)
        return render(request, 'customer/complain.html', {'username': username})
    except:
        return redirect('/common/login')

def showcomplain(request):
    # 获取信息
    username = request.session.get('username', '游客')
    user = User.objects.get(username=username)
    # 替换: 避免空格后的内容在后续get的时候丢掉
    title = request.POST.get('title').replace(" ", "_")
    content = request.POST.get('content')
    # 测试
    string = test()
    # 新建
    if user:
        try:
            complain = Complain.objects.get(title=title)
            complain.content = content
            return render(request, 'customer/complain.html', {'username': username, 'title': title, 'content': content, 'nlp_result': string})
        except:
            record = Complain.objects.create(user=user, title=title, content=content)
            return render(request, 'customer/complain.html', {'username': username, 'title': title, 'content': content, 'nlp_result': string})
    else:
        return redirect("/common/main")

